﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace SysManage.Module.Grassroots
{
    public partial class ItemEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "GrassrootsGroupAdd";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        public void BindData()
        {
            this.ppBranch.WindinsUrl = "../../../Share/Picker/BranchSinglePicker.aspx?BranchID=" + BranchID;
            //ppBranch.Text = ControlBusiness.GetBranchPickerText(BranchID);
            //ppBranch.Value = BranchID;
            if (!IsSystemAdmin)
            {
                this.ppBranch.Enabled = false;
                this.ppBranch.WindinsUrl = "../../../Share/Picker/BranchSinglePicker.aspx?BranchID=" + PartyOrg.QueryTopBranchId();
            }

        }

        private void LoadData()
        {
            var groupId = "";
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyGrassrootsGroupItem current = DB.PartyGrassrootsGroupItems.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                ControlBusiness.ControlBind(
              ddlTypeName, DB.PartyGrassrootsGroupItemTypes.Where(a => a.GroupID == current.GroupID), "TypeName", "ID");
                groupId = current.GroupID;
                txtItemName.Text = current.ItemName;
                txtLinkAddress.Text = current.LinkAddress;
                try
                {
                    txtLinkTime.Text = Convert.ToDateTime(current.LinkTime).ToString("yyyy-MM-dd");
                }
                catch (Exception)
                {
                    txtLinkTime.Text = "2010-01-01";
                }
                txtOrderNo.Text = current.OrderNo.ToString();
                cbxEnabled.Checked = current.State == 4;
                ppBranch.Text = ControlBusiness.GetBranchPickerText(current.BranchID);
                ppBranch.Value = current.BranchID;
                txtLinkMan.Text = current.LinkMan;
                txtLinkMobileNo.Text = current.LinkMobileNo;
                this.ddlTypeName.SelectedValue = current.TypeID;
            }
            else
            {
                groupId = GetQueryIntValue("GroupID");
                ControlBusiness.ControlBind(
     ddlTypeName, DB.PartyGrassrootsGroupItemTypes.Where(a => a.GroupID == groupId), "TypeName", "ID");
                if (groupId == "")
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
            }
            var group = DB.PartyGrassrootsGroups.Find(groupId);
            if (group == null)
            {
                // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            txtGroupName.Text = group.GroupName;
        }

        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            var current = DB.PartyGrassrootsGroupItems.Where(a => a.ItemName == this.txtItemName.Text.Trim() && a.ID != id);
            if (current.Any())
            {
                Alert.Show("小组名称已存在，请重新输入！", String.Empty);
                return;
            }
            var current2 = DB.PartyGrassrootsGroupItems.Where(a => a.BranchID == this.ppBranch.Value && a.ID != id);
            if (current.Any())
            {
                Alert.Show("所选择的组织已存在驻点小组，请重新选择的组织！", String.Empty);
                return;
            }
            PartyGrassrootsGroupItem item;

            if (id != "")
            {
                //更新
                item = DB.PartyGrassrootsGroupItems.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.LoginID;
            }
            else
            {

                item = new PartyGrassrootsGroupItem();
                item.ID = StringHelper.NewID;
                item.Description = "";
                item.CreateTime = DateTime.Now;
                item.ModifyTime = DateTime.Now;
                item.CreateBy = CurrentUser.LoginID;
                item.ModifyBy = CurrentUser.LoginID;
                DB.PartyGrassrootsGroupItems.Add(item);
                item.GroupID = GetQueryIntValue("GroupID");
            }


            item.TypeID = this.ddlTypeName.SelectedValue;
            item.BranchID = ppBranch.Value;
            item.ItemName = txtItemName.Text;
            item.State = cbxEnabled.Checked ? 4 : 1;
            item.LinkAddress = txtLinkAddress.Text;
            item.LinkTime = txtLinkTime.Text;
            item.OrderNo = Convert.ToInt32(txtOrderNo.Text);
            item.LinkMan = txtLinkMan.Text;
            item.LinkMobileNo = txtLinkMobileNo.Text;
            DB.SaveChanges();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

        }
        #endregion

    }
}
